﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using System.Media;
using System.Text.RegularExpressions;
using System.Linq;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucColdStorageLocation.xaml
    /// </summary>
    public partial class ucColdStorageLocation : UserControl
    {
        LibraryManagerInCom lm = new LibraryManagerInCom();
        Boolean cbxChange = true;
        public ucColdStorageLocation()
        {
            this.InitializeComponent();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();
        }

        private void loadData()
        {
            cbxChange = true;
            cbxDescription.Items.Clear();
            //cbxDescription.ItemsSource = LibraryManagerInCom.LocationList;
            foreach (var item in LibraryManagerInCom.LocationList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "location";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedItem != null)
            {
                txtAverage.Text = ((tb_location)cbxDescription.SelectedItem).average_temperature.ToString();
                txtMax.Text = ((tb_location)cbxDescription.SelectedItem).max_temperature.ToString();
                txtAverageMin.Text = ((tb_location)cbxDescription.SelectedItem).average_min_temperature.ToString();
                txtMin.Text = ((tb_location)cbxDescription.SelectedItem).min_temperature.ToString();

                txtAverage_Copy.Text = ((tb_location)cbxDescription.SelectedItem).average_humidity.ToString();
                txtMax_Copy.Text = ((tb_location)cbxDescription.SelectedItem).max_humidity.ToString();
                txtMin_Humid.Text = ((tb_location)cbxDescription.SelectedItem).min_humidity.ToString();
                txtAverage_Min_Humid.Text = ((tb_location)cbxDescription.SelectedItem).average_min_humidity.ToString();
            }
            else
            {
                //txtAverage.Text = "";
                //txtMax.Text = "";

                //txtAverage_Copy.Text = "";
                //txtMax_Copy.Text = "";
            }
        }

        private void enableButtons()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;

            cbxDescription.IsEditable = false;


        }
        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;

            cbxChange = false;

            cbxDescription.IsEditable = true;
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            txtAverage.Text = "";
            txtAverage_Copy.Text = "";
            txtMax.Text = "";
            txtMax_Copy.Text = "";
            txtMin.Text = "";
            txtAverageMin.Text = "";
            txtMin_Humid.Text = "";
            txtAverage_Min_Humid.Text = "";

            disableButtons();


        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_location lc = (tb_location)cbxDescription.SelectedItem;
            tb_location lc2 = LibraryManagerInCom.copyLocation(lc);

            cbxDescription.Text = lc2.location;
            txtMax.Text = lc2.max_temperature.ToString();
            txtAverage.Text = lc2.average_temperature.ToString();
            txtAverageMin.Text = lc2.average_min_temperature.ToString();
            txtMin.Text = lc2.min_temperature.ToString();

            txtMax_Copy.Text = lc2.max_humidity.ToString();
            txtAverage_Copy.Text = lc2.average_humidity.ToString();
            txtAverage_Min_Humid.Text = lc2.average_min_humidity.ToString();
            txtMin_Humid.Text = lc2.min_humidity.ToString();

            disableButtons();

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                try
                {
                    tb_location lc = (tb_location)cbxDescription.SelectedItem;
                    LibraryManagerInCom.delLocation(lc);

                    // Cập nhật lại dữ liệu cho control
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_location lc = new tb_location();
                lc.location = cbxDescription.Text;

                lc.max_temperature = Convert.ToDouble(txtMax.Text);
                lc.average_temperature = Convert.ToDouble(txtAverage.Text);
                lc.average_min_temperature = Convert.ToDouble(txtAverageMin.Text);
                lc.min_temperature = Convert.ToDouble(txtMin.Text);


                lc.max_humidity = Convert.ToDouble(txtMax_Copy.Text);
                lc.average_humidity = Convert.ToDouble(txtAverage_Copy.Text);
                lc.average_min_humidity = Convert.ToDouble(txtAverage_Min_Humid.Text);
                lc.min_humidity = Convert.ToDouble(txtMin_Humid.Text);

               
                    LibraryManagerInCom.addLocation(lc);
                    // Refesh form
                    enableButtons();
                    loadData();
               


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();
        }


        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion


        #region Check Input Digit
        private void txtMax_Copy_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }

        }

        private void txtAverage_Copy_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        private void txtMax_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }

        private void txtAverage_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion



    }
}